<?php 

require ('../base.php');

$query = $Db->table('goodsorders')->where('balance_time','<=',date('Y-m-d H:i:s' ))->where('blance_state','<>','1');
$list = $query->select();

foreach($list as $v){
	$Db->table('goodsorders')->where('balance_time','<=',date('Y-m-d H:i:s' ))->where('ordersid',$v['ordersid'])->update(['blance_state'=>'1']);//更新状态
	$money=$Db->table('shop')->where('shopid',$v['shopid'])->find()['money'];
	//结算店铺钱包
	$Db->table('shop')->where('shopid',$v['shopid'])->update(['money' => ($money + $v['blance_price'])]);
	//$balance=$Db->table('shop')->where('shopid',$v['shopid'])->fetchSql()->find();
	
	$numbers = $Db->table('goodsorders')->where('balance_time','<=',( date('Y-m-d H:i:s',time()) ))->where('ordersid',$v['ordersid'])->find()['numbers'];//查询结算订单号
	
	//插入钱包记录
	$resultId = $Db->table('shop_pay')->insertGetId([
		'sid'=>$v['shopid'], 'active_pay'=>3, 'amount'=>$v['blance_price'], 'status'=>1, 'style'=>4, 'numbers'=>$numbers,
		]);
	if($resultId) echo json_encode(['code'=>200,'msg'=>'成功','data'=>$resultId]);
}

